package com.invengo.module_check.ui.fragment.firstpos;

import static com.invengo.module_check.data.entity.ActionArgs.ACTION_FIRST;
import static com.invengo.module_check.data.entity.BundleKey.KEY_COUNT;
import static com.invengo.module_check.data.entity.BundleKey.KEY_INDEX;
import static com.invengo.module_check.data.entity.BundleKey.KEY_IS_SHELF_RESULT;
import static com.invengo.module_check.data.entity.BundleKey.KEY_OP_STATE;
import static com.invengo.module_check.data.entity.BundleKey.KEY_SHELFS;
import static com.invengo.module_check.data.entity.BundleKey.KEY_SHELF_CODE;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import androidx.viewpager.widget.ViewPager;

import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.entity.BundleKey;
import com.invengo.module_check.databinding.FragmentFirstBinding;
import com.invengo.module_check.ui.fragment.adapter.AlphaAndScalePageTransformer;
import com.invengo.module_check.ui.fragment.firstpos.fragment.FirstPosFragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.bus.Messenger;

public class FirstFragment extends BaseFragment<FragmentFirstBinding, FirstViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_first;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        String shelfCode = getArguments().getString(KEY_SHELF_CODE);
        viewModel.getShelfList(shelfCode);
    }

    @Override
    public void initViewObservable() {
        viewModel.ui.shelfListEvent.observe(this, shelfBeans -> {
            initViewPager(shelfBeans);
        });
        viewModel.ui.nextEvent.observe(this, shelfBeans -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable(KEY_SHELFS, (Serializable) shelfBeans);
            bundle.putSerializable(BundleKey.KEY_ACTION, ACTION_FIRST);
            bundle.putBoolean(KEY_OP_STATE, shelfBeans != null);
            bundle.putBoolean(KEY_IS_SHELF_RESULT, true);
            bundle.putString(BundleKey.KEY_TITLE, "首书定位");
            NavHostFragment.findNavController(this).navigate(ACTION_FIRST.getResultAction(), bundle);
        });
        viewModel.ui.scanBookEvent.observe(this, bean -> {
            BaseFragment baseFragment = fragmentList.get(binding.viewpager.getCurrentItem());
            Messenger.getDefault().send(bean, baseFragment.getClass().getSimpleName() + baseFragment.getTag());
        });
    }

    List<BaseFragment> fragmentList = new ArrayList<>();

    private void initViewPager(List<ShelfBean> beans) {
        for (int i = 0; i < beans.size(); i++) {
            FirstPosFragment firstPosFragment = new FirstPosFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(KEY_INDEX, i + 1);
            bundle.putInt(KEY_COUNT, beans.size());
            bundle.putSerializable(KEY_SHELFS, beans.get(i));
            firstPosFragment.setArguments(bundle);
            fragmentList.add(firstPosFragment);
        }
        MyFragmentStatePagerAdapter adapter = new MyFragmentStatePagerAdapter(getChildFragmentManager()) {
            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }
        };
        binding.viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewModel.stopScan();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        binding.viewpager.setOffscreenPageLimit(fragmentList.size() > 1 ? fragmentList.size() - 1 : 1);
//        binding.viewpager.setOffscreenPageLimit(fragmentList.size() > 1 ? fragmentList.size() - 1 : 1);
        binding.viewpager.setPageTransformer(true, new AlphaAndScalePageTransformer());
        binding.viewpager.setAdapter(adapter);
        binding.indicator.setViewPager(binding.viewpager);
        adapter.registerDataSetObserver(binding.indicator.getDataSetObserver());
    }
}
